/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * DataStreams.java
 *
 * Created on Oct 7, 2010, 2:04:04 AM
 */
package com.google.code.xbeejavaapi.configapp;

import com.google.code.xbeejavaapi.api.Filter;
import com.google.code.xbeejavaapi.api.LocalXBee;
import com.google.code.xbeejavaapi.api.XBeeAddress;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JTextField;
import org.apache.log4j.Logger;

/**
 *
 * @author David Miguel Antunes <davidmiguel [ at ] antunes.net>
 */
public class DataStreams extends javax.swing.JPanel {

    private static final Logger logger = Logger.getLogger(DataStreams.class);
    private LocalXBee xbee;
    private int counter = 1;

    /** Creates new form DataStreams */
    public DataStreams(LocalXBee xbee) {
        this.xbee = xbee;
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        streamsTabbedPane = new javax.swing.JTabbedPane();
        newStreamPanel = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        osAddHB = new javax.swing.JTextField();
        osAddLB = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        osSE = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        osDE = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        osCID = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        osPID = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        osBR = new javax.swing.JTextField();
        osARD = new javax.swing.JCheckBox();
        jPanel2 = new javax.swing.JPanel();
        jLabel8 = new javax.swing.JLabel();
        addCB = new javax.swing.JCheckBox();
        isAddHB = new javax.swing.JTextField();
        isAddLB = new javax.swing.JTextField();
        seCB = new javax.swing.JCheckBox();
        isSE = new javax.swing.JTextField();
        isDE = new javax.swing.JTextField();
        deCB = new javax.swing.JCheckBox();
        isCID = new javax.swing.JTextField();
        cidCB = new javax.swing.JCheckBox();
        isPID = new javax.swing.JTextField();
        pidCB = new javax.swing.JCheckBox();
        jButton1 = new javax.swing.JButton();

        setLayout(new javax.swing.BoxLayout(this, javax.swing.BoxLayout.LINE_AXIS));

        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Output Stream");

        jLabel2.setText("Address (hex):");

        osAddHB.setText("high bytes");

        osAddLB.setText("low bytes");

        jLabel3.setText("Source endpoint:");

        osSE.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                osSEKeyTyped(evt);
            }
        });

        jLabel4.setText("Destination endpoint:");

        jLabel5.setText("Cluster ID:");

        jLabel6.setText("Profile ID:");

        jLabel7.setText("Broadcast radius:");

        osARD.setText("Attempt route discovery");
        osARD.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
        osARD.setHorizontalTextPosition(javax.swing.SwingConstants.LEADING);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(osARD, javax.swing.GroupLayout.DEFAULT_SIZE, 334, Short.MAX_VALUE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 334, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(jPanel1Layout.createSequentialGroup()
                                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addGroup(jPanel1Layout.createSequentialGroup()
                                                    .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 148, Short.MAX_VALUE)
                                                    .addGap(18, 18, 18)
                                                    .addComponent(osAddHB, javax.swing.GroupLayout.DEFAULT_SIZE, 80, Short.MAX_VALUE))
                                                .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, 246, Short.MAX_VALUE))
                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED))
                                        .addGroup(jPanel1Layout.createSequentialGroup()
                                            .addComponent(jLabel4)
                                            .addGap(116, 116, 116)))
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addComponent(jLabel5)
                                        .addGap(187, 187, 187)))
                                .addGroup(jPanel1Layout.createSequentialGroup()
                                    .addComponent(jLabel6)
                                    .addGap(193, 193, 193)))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel7)
                                .addGap(141, 141, 141)))
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(osBR, javax.swing.GroupLayout.DEFAULT_SIZE, 76, Short.MAX_VALUE)
                            .addComponent(osPID, javax.swing.GroupLayout.DEFAULT_SIZE, 76, Short.MAX_VALUE)
                            .addComponent(osDE, javax.swing.GroupLayout.DEFAULT_SIZE, 76, Short.MAX_VALUE)
                            .addComponent(osAddLB, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 76, Short.MAX_VALUE)
                            .addComponent(osSE, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 76, Short.MAX_VALUE)
                            .addComponent(osCID, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 76, Short.MAX_VALUE))))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(osAddLB, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(osAddHB, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(osSE, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(osDE, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(osCID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(osPID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(osBR, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(osARD)
                .addContainerGap(16, Short.MAX_VALUE))
        );

        jLabel8.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel8.setText("Input Stream");

        addCB.setText("enabled");

        isAddHB.setText("high bytes");

        isAddLB.setText("low bytes");

        seCB.setText("enabled");

        deCB.setText("enabled");

        cidCB.setText("enabled");

        pidCB.setText("enabled");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel8, javax.swing.GroupLayout.DEFAULT_SIZE, 281, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(jPanel2Layout.createSequentialGroup()
                                    .addComponent(addCB)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                    .addComponent(isAddHB, javax.swing.GroupLayout.DEFAULT_SIZE, 102, Short.MAX_VALUE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED))
                                .addGroup(jPanel2Layout.createSequentialGroup()
                                    .addComponent(seCB)
                                    .addGap(126, 126, 126)))
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(cidCB)
                                    .addComponent(deCB)
                                    .addComponent(pidCB))
                                .addGap(126, 126, 126)))
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(isDE, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 80, Short.MAX_VALUE)
                            .addComponent(isSE, javax.swing.GroupLayout.DEFAULT_SIZE, 80, Short.MAX_VALUE)
                            .addComponent(isAddLB, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 80, Short.MAX_VALUE)
                            .addComponent(isCID, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 80, Short.MAX_VALUE)
                            .addComponent(isPID, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 80, Short.MAX_VALUE))))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel8)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(addCB)
                    .addComponent(isAddLB, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(isAddHB, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(seCB)
                    .addComponent(isSE, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(deCB)
                    .addComponent(isDE, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cidCB)
                    .addComponent(isCID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(pidCB)
                    .addComponent(isPID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(89, Short.MAX_VALUE))
        );

        jButton1.setText("Open Streams");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout newStreamPanelLayout = new javax.swing.GroupLayout(newStreamPanel);
        newStreamPanel.setLayout(newStreamPanelLayout);
        newStreamPanelLayout.setHorizontalGroup(
            newStreamPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, newStreamPanelLayout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, newStreamPanelLayout.createSequentialGroup()
                .addContainerGap(564, Short.MAX_VALUE)
                .addComponent(jButton1)
                .addContainerGap())
        );
        newStreamPanelLayout.setVerticalGroup(
            newStreamPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(newStreamPanelLayout.createSequentialGroup()
                .addGroup(newStreamPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 15, Short.MAX_VALUE)
                .addComponent(jButton1)
                .addContainerGap())
        );

        streamsTabbedPane.addTab("tab1", newStreamPanel);

        add(streamsTabbedPane);
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed

        try {
            LocalXBee.XBeeOutputStream outputStream;
            LocalXBee.XBeeInputStream inputStream;

            {
                XBeeAddress address = new XBeeAddress(parseField(osAddHB), parseField(osAddLB));
                long sourceEndpoint = parseField(osSE);
                long destinationEndpoint = parseField(osDE);
                long clusterId = parseField(osCID);
                long profileId = parseField(osPID);
                long maxBroadcastHops = parseField(osBR);
                boolean attemptRouteDiscovery = osARD.isSelected();

                outputStream = xbee.openOutputStream(address, sourceEndpoint, destinationEndpoint, clusterId, profileId, maxBroadcastHops, attemptRouteDiscovery);
            }
            {
                Filter filter = new Filter(
                        addCB.isSelected(), addCB.isSelected() ? new XBeeAddress(parseField(isAddHB), parseField(isAddLB)) : null,
                        seCB.isSelected(), seCB.isSelected() ? (int) parseField(isSE) : 0,
                        deCB.isSelected(), deCB.isSelected() ? (int) parseField(isDE) : 0,
                        cidCB.isSelected(), cidCB.isSelected() ? (int) parseField(isCID) : 0,
                        pidCB.isSelected(), pidCB.isSelected() ? (int) parseField(isPID) : 0);

                inputStream = xbee.openInputStream(filter);
            }

            JTextField[] osTextFields = new JTextField[]{osSE, osDE, osCID, osPID};
            final JTextField[] isTextFields = new JTextField[]{isSE, isDE, isCID, isPID};
            for (int i = 0; i < osTextFields.length; i++) {
                final JTextField jTextField = osTextFields[i];
                final int fi = i;
                jTextField.addActionListener(new ActionListener() {

                    public void actionPerformed(ActionEvent e) {
                        if (isTextFields[fi].getText().equals("")) {
                            isTextFields[fi].setText(jTextField.getText());
                        }
                    }
                });
            }

            DataStreamTab tab = new DataStreamTab(inputStream, outputStream, "OutputStream = " + outputStream + "\nInput Stream = " + inputStream);
            streamsTabbedPane.add("Streams "+counter++, tab);

        } catch (Exception e) {
            logger.error(e + " at " + e.getStackTrace()[0].toString());
        }

    }//GEN-LAST:event_jButton1ActionPerformed

    private long parseField(JTextField field) {
        String text = field.getText();
        text = text.replaceAll(" ", "");
        long value;
        if (text.startsWith("0x") || text.startsWith("0X")) {
            value = Long.parseLong(text.substring(2), 16);
        } else {
            value = Long.parseLong(text);
        }
        return value;
    }
    private void osSEKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_osSEKeyTyped
        // TODO add your handling code here:
    }//GEN-LAST:event_osSEKeyTyped
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox addCB;
    private javax.swing.JCheckBox cidCB;
    private javax.swing.JCheckBox deCB;
    private javax.swing.JTextField isAddHB;
    private javax.swing.JTextField isAddLB;
    private javax.swing.JTextField isCID;
    private javax.swing.JTextField isDE;
    private javax.swing.JTextField isPID;
    private javax.swing.JTextField isSE;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel newStreamPanel;
    private javax.swing.JCheckBox osARD;
    private javax.swing.JTextField osAddHB;
    private javax.swing.JTextField osAddLB;
    private javax.swing.JTextField osBR;
    private javax.swing.JTextField osCID;
    private javax.swing.JTextField osDE;
    private javax.swing.JTextField osPID;
    private javax.swing.JTextField osSE;
    private javax.swing.JCheckBox pidCB;
    private javax.swing.JCheckBox seCB;
    private javax.swing.JTabbedPane streamsTabbedPane;
    // End of variables declaration//GEN-END:variables
}
